//
//  ZYDiscountTagView.m
//  AudientList
//
//  Created by zhangweiwei on 16/6/16.
//  Copyright © 2016年 Erica. All rights reserved.
//

#import "ZYDiscountTagView.h"
#import "ZYDiscountTagCell.h"

@interface ZYDiscountTagView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation ZYDiscountTagView

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//        flowLayout.minimumInteritemSpacing = 5;
        flowLayout.minimumLineSpacing = 5;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ZYDiscountTagCell class]) bundle:nil] forCellWithReuseIdentifier:ZYDiscountTagCellReuseID];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.scrollEnabled = NO;
        
        
    }
    return _collectionView;
}

#pragma mark -初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
        
    }
    return self;
}

- (void)setupUI
{
    [self addSubview:self.collectionView];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.collectionView.frame = self.bounds;
    
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    CGFloat itemWH = self.collectionView.frame.size.height;
    flowLayout.itemSize = CGSizeMake(itemWH, itemWH);
    
    
}

- (void)setTagModels:(NSArray<ZYTagModel *> *)tagModels
{
    _tagModels = tagModels;
    
    [self.collectionView reloadData];
}

#pragma mark -UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.tagModels.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZYDiscountTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ZYDiscountTagCellReuseID forIndexPath:indexPath];
    
    cell.tagModel = self.tagModels[indexPath.item];
    
    return cell;
}

@end
